//
//  QHTableSubViewController.m
//  QHTableViewDemo
//
//  Created by chen on 17/3/21.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "QHTableSubViewController.h"

#import "QHMeshView.h"
#import "simplest_eagl_rgb_render.h"

@interface QHTableSubViewController ()

@property (nonatomic, strong) QHMeshView *meshV;
@property (nonatomic) CGPoint startP;

@property (nonatomic, strong) UIView *preview;
@property (nonatomic, strong) simplest_eagl_rgb_render *render;

@end

@implementation QHTableSubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIView *preview = [UIView new];
    preview.backgroundColor = [UIColor clearColor];
    preview.frame = CGRectMake(0, 0, 300, 270);
    preview.center = self.view.center;
    [self.view addSubview:preview];
    self.preview = preview;
    
    QHMeshView *meshV = [QHMeshView new];
    meshV.backgroundColor = [UIColor clearColor];
    meshV.frame = CGRectMake(0, 0, 300, 270);
    meshV.center = self.view.center;
    meshV.showMask = YES;
    [self.view addSubview:meshV];
    self.meshV = meshV;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(a:)];
    [self.meshV addGestureRecognizer:pan];
    
    UIButton *render_btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [render_btn setTitle:@"绘制" forState:UIControlStateNormal];
    render_btn.titleLabel.textColor = [UIColor orangeColor];
    [render_btn setBackgroundColor:[UIColor grayColor]];
    [render_btn addTarget:self action:@selector(renderAction) forControlEvents:UIControlEventTouchUpInside];
    render_btn.frame = CGRectMake(20, 90, 60, 30);
    [self.view addSubview:render_btn];
    
    UIButton *test_btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [test_btn setTitle:@"测试" forState:UIControlStateNormal];
    test_btn.titleLabel.textColor = [UIColor orangeColor];
    [test_btn setBackgroundColor:[UIColor grayColor]];
    [test_btn addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
    test_btn.frame = CGRectMake(CGRectGetMaxX(render_btn.frame) + 20, 90, 60, 30);
    [self.view addSubview:test_btn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)a:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        CGPoint sp = [pan locationInView:self.meshV];
        _startP = sp;
    }
    else if (pan.state == UIGestureRecognizerStateEnded) {
        CGPoint ep = [pan locationInView:self.meshV];
        [self.meshV warp2Start:_startP end:ep];
        self.render.v_mesh_a = self.meshV.v_mesh;
        self.render.h_pt_a = self.meshV.h_pt;
        [self.render captureOutput];
    }
}

- (void)renderAction {
    if (self.render != nil) {
        return;
    }
    self.render = [simplest_eagl_rgb_render createWith:self.preview];
    self.render.v_mesh_a = self.meshV.v_mesh;
    self.render.h_pt_a = self.meshV.h_pt;
    [self.render captureOutput];
}

- (void)testAction {
    [self.meshV testIdx];
}

@end
